<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:userPrivilege:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userPrivilegeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="特权名字" align="center" prop="name" />
      <!-- <el-table-column label="特权唯一字段" align="center" prop="privilegeKey" /> -->
      <el-table-column label="图标" align="center" prop="privilegeImg">
        <template slot-scope="scope">
          <el-image v-if="scope.row.privilegeImg" style="width: 50px; height: 50px" 
          :src="scope.row.privilegeImg" :preview-src-list="[scope.row.privilegeImg]" />
        </template>
      </el-table-column>
      <el-table-column label="特权说明" align="center" prop="remarks" :show-overflow-tooltip="true"/>
      <el-table-column label="最后编辑时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上线/下线" align="center" prop="status" >
       <template slot-scope="scope">
          <span>{{ scope.row.status===0?"下线":scope.row.status===1?'上架':'删除'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:userPrivilege:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:userPrivilege:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员等级特权对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="特权名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入特权名字" />
        </el-form-item>
        <!-- <el-form-item label="特权唯一字段" prop="privilegeKey">
          <el-input v-model="form.privilegeKey" placeholder="请输入特权唯一字段" />
        </el-form-item> -->
        <el-form-item label="图标" prop="privilegeImg">
          <SingleUpload :avatar="form.privilegeImg" @emitSetUrl="setUrl" />
        </el-form-item>
        <el-form-item label="特权说明" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入特权说明" type="textarea" :rows="3"/>
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">上线</el-radio>
            <el-radio :label="0">下线</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserPrivilege, getUserPrivilege, delUserPrivilege, addUserPrivilege, updateUserPrivilege, exportUserPrivilege } from "@/api/system/userPrivilege";
import SingleUpload from '@/components/Upload/SingleUpload'
export default {
  name: "UserPrivilege",
  components:{SingleUpload},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 会员等级特权表格数据
      userPrivilegeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        privilegeKey: undefined,
        privilegeImg: undefined,
        remarks: undefined,
        createDate: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "特权名字不能为空", trigger: "blur" }
        ],
        privilegeImg: [
          { required: true, message: "图标不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    setUrl(data){
      this.form.privilegeImg = data.url
    },
    /** 查询会员等级特权列表 */
    getList() {
      this.loading = true;
      listUserPrivilege(this.queryParams).then(response => {
        this.userPrivilegeList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        privilegeKey: undefined,
        privilegeImg: undefined,
        remarks: undefined,
        createDate: undefined,
        createBy: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员等级特权";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserPrivilege(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员等级特权";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserPrivilege(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUserPrivilege(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会员等级特权编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserPrivilege(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员等级特权数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserPrivilege(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>